﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using HuaCheng.Model;
using HuaCheng.DataAccess;
using HuaCheng.Controller;

namespace HuaCheng.UserControls
{
    /// <summary>
    /// 动态曲线图
    /// </summary>
    public partial class UserPicture : UserBase
    {
        public DateTime time;
        //控制显示的标志
        public int i = 0;
        //synObj对象使用lock关键字用于实现线程同步
        private static object synObj = new object();
        private bool isOver = true;
        //定义表格中实际点的个数
        private DataTable pointDB = UserPictureControl.CreateTable();
        //private DataTable pointView;
        public UserPicture()
        {
            InitializeComponent();
            InitChart();
        }

        /// <summary>
        /// 通道数据类
        /// </summary>
        public override Channel Channel
        {
            get { return channel; }
            set
            {
                if (isOver)
                {
                    isOver = false;
                    channel = value;
                    //Console.WriteLine("StepTime"+value.StepTime);
                    string title = value.Title == "" ? value.ChId : value.Title;
                    //Console.WriteLine(value.DevIp + "通道"+value.Cid);
                    //对i进行原子操作
                    System.Threading.Interlocked.Increment(ref i);
                    chartChlTimerSet(title, time.AddMilliseconds(i * 10), value.Voltage, value.Current, i);
                    //Console.WriteLine(temp.Second + "." + temp.Millisecond + ":" + value.Current);   
                }
            }
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClick(e);
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }
        /// <summary>
        /// 动态曲线绘制
        /// </summary>
        public void chartChlTimerSet(string title, DateTime nowTime, float voltage, float current, int i)
        {
            lock (synObj)
            {
                // 最小值和最大值之间的差值
                TimeSpan timeLength = new TimeSpan(0, 0, 0, 4, 990);

                //Console.WriteLine("修改数据源");
                this.pointDB = UserPictureControl.UpdateTable(this.pointDB, nowTime, voltage, current);

                if (this.pointDB.Rows.Count == 0)
                {
                    //x轴的mininum设置后，其实就是x轴的源点
                    chartChl.ChartAreas[0].AxisX.Minimum = nowTime.ToOADate();
                    chartChl.ChartAreas[0].AxisX.Maximum = nowTime.Add(timeLength).ToOADate();
                }


                if (i % 25 == 0)
                {

                    DataTable pointView = UserPictureControl.CopyTable(this.pointDB);
                    //pointView = UserPictureControl.DeepCopy(this.pointDB);//深度复制

                    if (this.pointDB.Rows.Count == 500)
                    {
                        chartChl.ChartAreas[0].AxisX.Minimum = nowTime.Subtract(timeLength).ToOADate();
                        chartChl.ChartAreas[0].AxisX.Maximum = nowTime.ToOADate();
                    }
                    //给chart绑定数据源

                    bindData(pointView);
                    //Console.WriteLine("绑定数据源");

                    this.chartChl.Titles.Clear();
                    this.chartChl.Titles.Add(title);
                }
                isOver = true;
            }
        }
        private void InitParams()
        {
            i = 0;
            pointDB.Clear();
            time = System.DateTime.Now;
            time = time.AddMilliseconds(400 - time.Millisecond);
        }
        /// <summary>
        /// 初始化动态曲线
        /// </summary>
        private void InitChart()
        {
            InitParams();
            // 清空图表区域
            this.chartChl.ChartAreas.Clear();
            // 定义图表区域C1
            ChartArea chartChlArea1 = new ChartArea("C1");
            // 将图表区域C1添加到图表中
            this.chartChl.ChartAreas.Add(chartChlArea1);

            //X轴、Y轴标题
            this.chartChl.ChartAreas[0].AxisX.Title = "时间";
            this.chartChl.ChartAreas[0].AxisY.Title = "电压";
            this.chartChl.ChartAreas[0].AxisY2.Title = "电流";

            //设置游标
            this.chartChl.ChartAreas[0].CursorX.IsUserEnabled = false;
            this.chartChl.ChartAreas[0].CursorX.AutoScroll = false;
            this.chartChl.ChartAreas[0].CursorX.IsUserSelectionEnabled = false;
            this.chartChl.ChartAreas[0].CursorY.IsUserEnabled = false;
            this.chartChl.ChartAreas[0].CursorY.AutoScroll = false;
            this.chartChl.ChartAreas[0].CursorY.IsUserSelectionEnabled = false;

            //将滚动条放到图表外
            this.chartChl.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
            this.chartChl.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = false;

            // 设置滚动条的大小
            this.chartChl.ChartAreas[0].AxisX.ScrollBar.Size = 20;
            this.chartChl.ChartAreas[0].AxisY.ScrollBar.Size = 20;

            // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
            this.chartChl.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;
            this.chartChl.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.SkyBlue;

            // 设置自动放大与缩小的最小量
            // this.chartChl.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN;
            // this.chartChl.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;

            // 隐藏滚动条
            this.chartChl.ChartAreas[0].AxisX.ScrollBar.Enabled = false;
            this.chartChl.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
            this.chartChl.ChartAreas[0].AxisY2.ScrollBar.Enabled = false;

            // 设置图表区
            this.chartChl.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            this.chartChl.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
            this.chartChl.ChartAreas[0].AxisY2.ScaleView.Zoomable = true;
            this.chartChl.ChartAreas[0].CursorX.Interval = 0;
            this.chartChl.ChartAreas[0].CursorX.IntervalOffset = 0;
            this.chartChl.ChartAreas[0].CursorY.Interval = 0;
            this.chartChl.ChartAreas[0].CursorY.IntervalOffset = 0;

            // 设置坐标轴颜色
            this.chartChl.ChartAreas[0].AxisY.LineColor = Color.Red;
            this.chartChl.ChartAreas[0].AxisY2.LineColor = Color.Blue;
            this.chartChl.ChartAreas[0].AxisY.LineWidth = 1;
            this.chartChl.ChartAreas[0].AxisY2.LineWidth = 1;

            //设置X轴Y轴上格网
            this.chartChl.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;

            // 关闭副网格
            this.chartChl.ChartAreas[0].AxisX.MinorTickMark.Enabled = false;
            this.chartChl.ChartAreas[0].AxisY.MinorTickMark.Enabled = false;

            // 网格类型 短横线
            this.chartChl.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            this.chartChl.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            this.chartChl.ChartAreas[0].AxisY2.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

            //设置X轴Y轴的网格颜色
            this.chartChl.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
            this.chartChl.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
            this.chartChl.ChartAreas[0].AxisY2.MajorGrid.LineColor = System.Drawing.Color.Silver;

            // 开启主刻度和副刻度
            this.chartChl.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
            this.chartChl.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY.MajorTickMark.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY.MinorTickMark.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY2.MajorTickMark.Enabled = true;
            this.chartChl.ChartAreas[0].AxisY2.MinorTickMark.Enabled = true;

            // 设置主刻度和副刻度的颜色
            this.chartChl.ChartAreas[0].AxisY.MajorTickMark.LineColor = Color.Red;
            this.chartChl.ChartAreas[0].AxisY.MinorTickMark.LineColor = Color.Red;
            this.chartChl.ChartAreas[0].AxisY2.MajorTickMark.LineColor = Color.Blue;
            this.chartChl.ChartAreas[0].AxisY2.MinorTickMark.LineColor = Color.Blue;

            //设置轴标题方向
            this.chartChl.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Horizontal;
            this.chartChl.ChartAreas[0].AxisY2.TextOrientation = TextOrientation.Horizontal;

            // 设置轴标题对齐方式
            this.chartChl.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;
            this.chartChl.ChartAreas[0].AxisY2.TitleAlignment = StringAlignment.Far;

            this.chartChl.ChartAreas[0].AxisY.TitleForeColor = Color.Red;
            this.chartChl.ChartAreas[0].AxisY2.TitleForeColor = Color.Blue;

            // 设定轴刻度值格式
            this.chartChl.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss";
            this.chartChl.ChartAreas[0].AxisY.LabelStyle.Format = "N2";
            this.chartChl.ChartAreas[0].AxisY2.LabelStyle.Format = "N2";

            //设置坐标轴范围
            this.chartChl.ChartAreas[0].AxisY.Maximum = Convert.ToSingle(DicHelper.RetrieveVolUpper());
            this.chartChl.ChartAreas[0].AxisY.Minimum = Convert.ToSingle(DicHelper.RetrieveVolLower());
            this.chartChl.ChartAreas[0].AxisY2.Maximum = Convert.ToSingle(DicHelper.RetrieveCurrUpper());
            this.chartChl.ChartAreas[0].AxisY2.Minimum = Convert.ToSingle(DicHelper.RetrieveCurrLower());
            this.chartChl.ChartAreas[0].AxisY.Interval = 1;
            this.chartChl.ChartAreas[0].AxisY2.Interval = 20;
            TimeSpan maxTime = new TimeSpan(0, 0, 5);
            this.chartChl.ChartAreas[0].AxisX.Maximum = time.Add(maxTime).ToOADate();
            this.chartChl.ChartAreas[0].AxisX.Minimum = time.ToOADate();
            this.chartChl.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Milliseconds;
            this.chartChl.ChartAreas[0].AxisX.Interval = 1000;
            this.chartChl.ChartAreas[0].Position.Width = 100;
            this.chartChl.ChartAreas[0].Position.Height = 100;
            this.chartChl.ChartAreas[0].Position.X = 0;
            this.chartChl.ChartAreas[0].Position.Y = 20;


            // 清空线条
            this.chartChl.Series.Clear();

            // 创建线条
            Series series1 = new Series("时间" + "-" + "电压");
            series1.ChartArea = "C1";
            this.chartChl.Series.Add(series1);
            Series series2 = new Series("时间" + "-" + "电流");
            series2.ChartArea = "C1";
            this.chartChl.Series.Add(series2);


            // 设置线条属性
            // 设置曲线类型
            this.chartChl.Series[0].ChartType = SeriesChartType.Line;
            this.chartChl.Series[1].ChartType = SeriesChartType.Line;
            // 设置曲线的颜色
            this.chartChl.Series[0].Color = Color.Red;
            this.chartChl.Series[1].Color = Color.Blue;
            // 设置曲线宽度
            this.chartChl.Series[0].BorderWidth = 1;
            this.chartChl.Series[1].BorderWidth = 1;
            //设置是否显示坐标标注
            //this.chartChl.Series[0].IsValueShownAsLabel = true;

            //设置线条的轴类型   主要设置以下方做X轴还是上方做X轴，左方做Y轴还是右方做Y轴
            this.chartChl.Series[0].XAxisType = AxisType.Primary;
            this.chartChl.Series[0].YAxisType = AxisType.Primary;
            this.chartChl.Series[1].XAxisType = AxisType.Primary;
            this.chartChl.Series[1].YAxisType = AxisType.Secondary;

            // 清空X、Y值数据
            this.chartChl.Series[0].Points.Clear();
            this.chartChl.Series[1].Points.Clear();

            // 设定X轴数据类型
            this.chartChl.Series[0].XValueType = ChartValueType.Time;
            this.chartChl.Series[1].XValueType = ChartValueType.Time;
            this.chartChl.Series[0].YValueType = ChartValueType.Double;
            this.chartChl.Series[1].YValueType = ChartValueType.Double;


            //绑定数据
            DataTable pointView = UserPictureControl.CopyTable(this.pointDB);
            bindData(pointView);

        }
        /// <summary>
        /// 清空原图
        /// </summary>
        public void Clear()
        {
            // 清空图表区域
            foreach (var series in chartChl.Series)
            {
                series.Points.Clear();
            }
            //chartChl.ChartAreas.Clear();
            InitParams();
            this.chartChl.ChartAreas[0].AxisX.Maximum = time.Add(new TimeSpan(0, 0, 5)).ToOADate();
            this.chartChl.ChartAreas[0].AxisX.Minimum = time.ToOADate();
        }

        public void bindData(DataTable pointView)
        {
            this.chartChl.DataSource = pointView;

            //给chart的series绑定数据
            this.chartChl.Series[0].XValueMember = pointView.Columns[0].ColumnName;
            this.chartChl.Series[0].YValueMembers = pointView.Columns[1].ColumnName;

            this.chartChl.Series[1].XValueMember = pointView.Columns[0].ColumnName;
            this.chartChl.Series[1].YValueMembers = pointView.Columns[2].ColumnName;

        }

    }
}
